#!/bin/sh

# Startup script for Xymon
#
# This starts the "xymonlaunch" tool, which in turn starts
# all of the other Xymon server programs.

case "`uname -s`" in
   "SunOS")
   	ID=/usr/xpg4/bin/id
	;;
   *)
   	ID=id
	;;
esac

if test `$ID -un` != @XYMONUSER@
then
	echo "Xymon must be started as the @XYMONUSER@ user"
	exit 1
fi

case "$1" in
   "start")
	if test -s @XYMONLOGDIR@/xymonlaunch.pid
	then
		kill -0 `cat @XYMONLOGDIR@/xymonlaunch.pid`
		if test $? -eq 0
		then
			echo "Xymon appears to be running, doing restart"
			$0 stop
		else
			rm -f @XYMONLOGDIR@/xymonlaunch.pid
		fi
	fi

	@RUNTIMEDEFS@ @XYMONHOME@/bin/xymonlaunch --config=@XYMONHOME@/etc/tasks.cfg --env=@XYMONHOME@/etc/xymonserver.cfg --log=@XYMONLOGDIR@/xymonlaunch.log --pidfile=@XYMONLOGDIR@/xymonlaunch.pid
	echo "Xymon started"
	;;

   "stop")
	if test -s @XYMONLOGDIR@/xymonlaunch.pid
	then
		kill -TERM `cat @XYMONLOGDIR@/xymonlaunch.pid`
		echo "Xymon stopped"
	else
		echo "Xymon is not running"
	fi
	rm -f @XYMONLOGDIR@/xymonlaunch.pid
	;;

   "status")
	if test -s @XYMONLOGDIR@/xymonlaunch.pid
	then
		kill -0 `cat @XYMONLOGDIR@/xymonlaunch.pid`
		if test $? -eq 0
		then
			echo "Xymon (xymonlaunch) running with PID `cat @XYMONLOGDIR@/xymonlaunch.pid`"
		else
			echo "Xymon not running, removing stale PID file"
			rm -f @XYMONLOGDIR@/xymonlaunch.pid
		fi
	else
		echo "Xymon (xymonlaunch) does not appear to be running"
	fi
	;;

   "restart")
	if test -s @XYMONLOGDIR@/xymonlaunch.pid
	then
		$0 stop
		sleep 10
		$0 start
	else
		echo "xymonlaunch does not appear to be running, starting it"
		$0 start
	fi
	;;

   "reload")
	if test -s @XYMONLOGDIR@/xymond.pid
	then
		kill -HUP `cat @XYMONLOGDIR@/xymond.pid`
	else
		echo "xymond not running (no PID file)"
	fi
	;;

   "rotate")
   	for PIDFILE in @XYMONLOGDIR@/*.pid
	do
		kill -HUP `cat $PIDFILE`
	done
	;;

   *)
   	echo "Usage: $0 start|stop|restart|reload|status|rotate"
	break;
esac

exit 0

